package com.xoms.pad.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xoms.pad.controller.res.ListResult;
import com.xoms.pad.controller.res.ObjectResult;
import com.xoms.pad.controller.res.bean.RProCategory3Bean;
import com.xoms.pad.controller.res.bean.RProCategoryJSONBean;
import com.xoms.pad.controller.res.bean.RProductBean;
import com.xoms.pad.controller.springmvc.req.GetAllProductsParam;
import com.xoms.pad.controller.springmvc.req.GetCate3ProductParam;
import com.xoms.pad.model.AccountBean;
import com.xoms.pad.service.ProCategoryService;
import com.xoms.pad.service.ProductService;
import com.xoms.pad.service.exception.ServiceException;

/**
 * 产品接口
 * 
 * @author stallmandu
 *
 */
@Component
public class ProductController {

	@Autowired
	ProCategoryService proCategoryServiceImpl;

	@Autowired
	ProductService productServiceImpl;

	/**
	 * 获取所有商品分类列表1、2级
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<RProCategoryJSONBean> getAllCategories(HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<RProCategoryJSONBean> result = new ObjectResult<>();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");
			// 获取全部商品分类列表
			RProCategoryJSONBean resData = proCategoryServiceImpl.getAllCategories(account);
			if (resData != null) {
				result.setData(resData);
			}
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	
	
	
	/**
	 * 获取商品列表3级
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RProCategory3Bean> getCate3Product(GetCate3ProductParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<RProCategory3Bean> result = new ListResult<>();
		
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");
			
//			int page = NumUtil.toInt(param.getPage(), 0);
//			int PageSize = NumUtil.toInt(param.getPageSize(), 20);
			
			String cid = param.getCid();
			
			
			List<RProCategory3Bean> resData = proCategoryServiceImpl.getCate3Product(account.getAccid(), cid);
			
			
			if (resData != null) {
				result.setData(resData);
				result.setTotalSize(resData.size());
			}
			
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	/**
	 * 获取商品列表
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RProductBean> getProducts(GetAllProductsParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<RProductBean> result = new ListResult<>();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");
			
//			int page = NumUtil.toInt(param.getPage(), 0);
//			int PageSize = NumUtil.toInt(param.getPageSize(), 20);
			
			String pid = param.getPid();
			String name = param.getName();
			
			
			
			List<RProductBean> resData = productServiceImpl.getProducts(account.getAccid(),pid,name);
			
			
			if (resData != null) {
				result.setData(resData);
				result.setTotalSize(resData.size());
			}
			
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}

	
}
